import {Component, Input, OnInit} from '@angular/core';
import {WmsStockPut, WmsStockPutCondition} from '../../wms-stock-put.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyWmsStockPutComponent} from '../modify-wms-stock-put.component';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {WmsWarehouseManageCondition} from '../../../wms-warehouse-manage/wms-warehouse-manage.type';
import {BaseGoodsCondition} from '../../../../goods-management/goo-base-goods/base-goods.type';
import {GooBaseGoodsSpecificationCondition} from '../../../../goods-management/goo-base-goods-specification/goo-base-goods-specification.type';
import {WmsStockDetail} from '../../../wms-stock-detail/wms-stock-detail.type';

@Component({
    selector: 'app-main-modify-wms-stock-put',
    templateUrl: './main-modify-wms-stock-put.component.html',
    styleUrls: ['./main-modify-wms-stock-put.component.css']
})
export class MainModifyWmsStockPutComponent implements OnInit {

    errRes = new ErrRes();

    domain: WmsStockPut = new WmsStockPut();

    selectedBaseGoods: any;

    selectedGooBaseGoodsSpecification: any;

    wmsStockDetailMap = new Map<string, WmsStockDetail>();

    wmsStockDetailList = new Array<WmsStockDetail>();

    extend = {
        domain: {
            wmsStockPutTypes: {
                items: [],
                nickname: '入库类型',
                change: (event) => {
                    console.log(event);
                }
            },
            wmsWareHouseManage: {
                items: [],
                nickname: '仓库名称',
                change: (event) => {
                    console.log(event);
                    this.domain.wmsWarehouseManageName = event.name;
                }
            },
            gooBaseGoods: {
               items: [],
               nickname: '基础商品',
               change: (event) => {
                   this.selectedBaseGoods = event;
                   const condition = new GooBaseGoodsSpecificationCondition();
                   condition.fkGooBaseGoodsUuid = event.id;
                   Services.gooBaseGoodsSpecification.findByFkGooBaseGoodsUuid(condition).subscribe(item => {
                       if (!isNullOrUndefined(item) && item.code === 'success') {
                           console.log(item);
                           this.extend.domain.gooBaseGoodsSpecification.items = [];
                           item.domains.forEach(gooBaseGoodsSpecification => {
                               this.extend.domain.gooBaseGoodsSpecification.items.push(
                                   {id: gooBaseGoodsSpecification.uuid, name: gooBaseGoodsSpecification.serName}
                               );
                           });
                       }
                   });
               },
            },
            gooBaseGoodsSpecification: {
               items: [],
               nickname: '商品规格',
               change: (event) => {
                    console.log(event);
                   this.selectedGooBaseGoodsSpecification = event;
               },
            },
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            serInputGroupData: {
                icon: fa.faCalendar,
                nickname: '日期',
                value: () => {
                    return this.domain.serInputGroupData;
                },
                focus: (event) => {
                    this.domain.serInputGroupData = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: WmsStockPutCondition = new WmsStockPutCondition();
            condition.uuid = uuid;
            Services.wmsStockPut.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
                    item.domain.stockDetailList.forEach(wmsStockDetail => {
                        this.wmsStockDetailMap.set(wmsStockDetail.serSpecificationUuid, wmsStockDetail);
                    });
                }
            });
        },
        update: {
            req: (event) => {
                console.log(event);
                console.log(this.wmsStockDetailMap);
                this.wmsStockDetailList = Array.from(this.wmsStockDetailMap.values());
                const wmsStockPut = Object.assign({stockDetailList: this.wmsStockDetailList}, this.domain);
                console.log(wmsStockPut);
                Services.wmsStockPut.batchSave(wmsStockPut, this.func.update.callback).subscribe();
                // Services.wmsStockPut.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.wmsStockPut.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        addGooBaseGoodsSpecification: () => {
            if (!isNullOrUndefined(this.selectedBaseGoods) && !isNullOrUndefined(this.selectedGooBaseGoodsSpecification)) {
                const wmsStockDetail = new WmsStockDetail();
                wmsStockDetail.serItemUuid = this.selectedBaseGoods.id;
                wmsStockDetail.serItemName = this.selectedBaseGoods.name;
                wmsStockDetail.serSpecificationName = this.selectedGooBaseGoodsSpecification.name;
                wmsStockDetail.serSpecificationUuid = this.selectedGooBaseGoodsSpecification.id;
                wmsStockDetail.count = 0;

                const baseGoodsCondition = new BaseGoodsCondition();
                baseGoodsCondition.uuid = this.selectedBaseGoods.id;
                Services.baseGoods.info(baseGoodsCondition).subscribe(goods => {
                    if (!isNullOrUndefined(goods) && goods.code === 'success') {
                        wmsStockDetail.serItemImg = goods.domain.serItemImg;
                        wmsStockDetail.serItemSerNum = goods.domain.serNum;
                        wmsStockDetail.serItemBrandUuid = goods.domain.serGoodsBarndId;
                        wmsStockDetail.serItemBrandName = goods.domain.serGoodsBrandName;
                        wmsStockDetail.wmsStockType = 'STOCK_PUT';
                    }
                });
                console.log(wmsStockDetail);
                this.wmsStockDetailMap.set(wmsStockDetail.serSpecificationUuid, wmsStockDetail);
            }
        },
        deleteGooBaseGoodsSpecification: (event) => {
            this.wmsStockDetailMap.delete(event);
        }
    };

    constructor(public modifyWmsStockPut: ModifyWmsStockPutComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyWmsStockPut.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        this.extend.domain.wmsStockPutTypes.items = [];
        Zero3Enum.WMS_STOCK_PUT_TYPE_FLAG.forEach((value, key) => {
            this.extend.domain.wmsStockPutTypes.items.push({id: key, name: value});
        });
        Services.wmsWarehouseManage.list(new WmsWarehouseManageCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                console.log(item);
                this.extend.domain.wmsWareHouseManage.items = [];
                item.page.content.forEach(wmsWarehouseManage => {
                    this.extend.domain.wmsWareHouseManage.items.push(
                        {id: wmsWarehouseManage.uuid, name: wmsWarehouseManage.serName}
                    );
                });
            }
        });
        Services.baseGoods.list(new BaseGoodsCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.gooBaseGoods.items = [];
                item.page.content.forEach(baseGoods => {
                    this.extend.domain.gooBaseGoods.items.push(
                        {id: baseGoods.uuid, name: baseGoods.serGoodsName}
                    );
                });
            }
        });
    }

}
